package module.avatar
{
	import common.resource.loaders.AnimatorLoader;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import utils.WalkingUtil;
	

	public class Avatar extends Sprite
	{
		public static var animalDely:int = 4;
		private var _resLoader:AnimatorLoader;
		private var _line:int = 0;
		private var _speed:Number = 4;
		private var _avatarPartArray:Array;
		private var _isWalking:Boolean = false;
		private var _frame:int;
		private var _dely:int = 0;
		/**
		 * 是否正在做行走和站立外的动作 
		 */		
		private var _isDoingAction:Boolean = false;
		
		//角色方向
		private var _direction:uint;
		
		public function Avatar()
		{
			_resLoader = new AnimatorLoader("assets/avatarPart/","2");
			_resLoader.addEventListener(Event.COMPLETE, onResOk);
			_resLoader.load();
		}
		
		private function onResOk(e:Event):void
		{
			addEventListener(Event.ENTER_FRAME, onEf);
		}
		
		private function onEf(e:Event):void
		{
			if(_dely > animalDely)
			{
				_dely = 0;
				rander();
			}
			_dely ++;
		}
		
		/**
		 * 行走 
		 */		
		public function walk():void
		{
			_isWalking = true;
		}
		
		/**
		 * 站立
		 */		
		public function stand():void
		{
			_isWalking = false;
		}
		
		/**
		 * 物理攻击 
		 */		
		public function attack():void
		{
			
		}
		
		/**
		 * 使用魔法 
		 */		
		public function magic():void
		{
			if(!_isDoingAction)
			{
				_isDoingAction = true;
				_line = 20;
			}
		}
		
		public function rander():void
		{
			
			if(_frame > 7)
				_frame = 0;
			while(numChildren > 0)
				removeChildAt(0);
			addChild(_resLoader.bmpDic[_frame + _direction * 8]);
			_frame ++;
			
			if(_isWalking)
			{
				this.x += _speed * WalkingUtil.getXIsAddByDir(_direction);
				this.y += _speed * WalkingUtil.getYIsAddByDir(_direction);
			}
			
		}
		
		/**
		 * 获得当前帧数 
		 */		
		private function getCurrentFrame():int
		{
			return _avatarPartArray[0].currentFrame;
		}

		public function get direction():uint
		{
			return _direction;
		}

		/**
		 * 方向
		 * @param value 
		 */		
		public function set direction(value:uint):void
		{
			_direction = value;
		}

		/**
		 * 是否正在行走
		 * @return  
		 */		
		public function get isWalking():Boolean
		{
			return _isWalking;
		}


		//
	}
}